package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"yk3.19/api/request"
	"yk3.19/global"
	"yk3.19/internal/model"
)

func VideoAdd(c *gin.Context) {
	var req request.VideoAdd
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	video := model.Video{
		UserId:   c.GetUint("userId"),
		Title:    req.Title,
		VideoUrl: req.VideoUrl,
		LikeNum:  req.LikeNum,
	}
	global.DB.Create(&video)
	if video.ID == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "短视频添加失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "短视频添加成功",
		"data": video,
	})
}

func VideoList(c *gin.Context) {
	var video []model.Video
	err := global.DB.Find(&video).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "短视频列表查询失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "短视频列表查询成功",
		"data": video,
	})
}

func VideoFindVideo(c *gin.Context) {
	var req request.VideoFindVideo
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var video []model.Video
	err = global.DB.Where("title like?", "%"+req.Title+"%").Find(&video).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "没有您要查找的短视频",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查找成功",
		"data": video,
	})
	return
}

func VideoInfo(c *gin.Context) {
	var req request.VideoInfo
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var video model.Video
	global.DB.Where("id = ?", req.VideoId).Find(&video)
	if video.ID == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "视频不存在",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "视频详情展示成功",
		"data": video,
	})
}
